﻿using System;
using System.Linq;
using NDBGen.Descriptors.Columns.DataTypes;
using NDBGen.Descriptors.Columns.Tokens;
using NDBGen.Plugins.DataAccess;

namespace NDBGen.Plugins.Descriptors.Columns.DataTypes
{
    [ColumnDataTypeCodeSnippetGenerator(typeof(RowVersionDataTypeDescriptor))]
    public sealed class DotNetRowVersionDataTypeCodeGenerator : DotNetColumnDataTypeCodeGeneratorBase<RowVersionDataTypeDescriptor>
    {
        private const String DBTYPE_FORMAT = "timestamp";
        private const String CSHARP_PROPERT_BODY_FORMAT = "public Byte[] {0} {{ get; set; }}";

        protected override Object[] GenerateColumnAttributeProperties()
        {
            var nullableDescriptor = this.Column.Descriptors.OfType<NullableTokenDescriptor>().Single();
            return new Object[]
            {
                ColumnMetadataHelper.ColumnAttribute.DbType.Format.SetArgs(DBTYPE_FORMAT, nullableDescriptor.IsActive ? String.Empty : " not"),
                ColumnMetadataHelper.ColumnAttribute.AutoSync.Always,
                ColumnMetadataHelper.ColumnAttribute.IsDbGenerated.True,
                ColumnMetadataHelper.ColumnAttribute.IsVersion.True
            };
        }

        protected override void WriteProperty(IIndentedTextWriter writer)
        {
            var property = String.Format(CSHARP_PROPERT_BODY_FORMAT, this.Column.Name.ToPascalCase());
            writer.WriteLine(property);
        }
    }
}